package com.example.springmvcdemo1.controller;

import com.example.springmvcdemo1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class LoginController {
    // 访问http://localhost:8080/login 打开登录页面
    @RequestMapping("/login")
    public RedirectView login(){
        //thymeleaf模板引擎会在resources/templates文件夹下寻找login.html文件，并返回给浏览器
        return new RedirectView("login.html");
    }
    //接收前端发送过来的登陆数据
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public RedirectView login(@RequestParam("username") String name, String password){
        //判断用户名密码是否正确
        if("admin".equals(name) && "123".equals(password)){
            //登录成功，跳转到index.html页面
            return new RedirectView("index.html");
        }
        //登录失败，跳转到login.html页面
        return new RedirectView("login.html");
    }

    //接收前端发送过来的登陆数据,方法形参是实体类的情况
    @RequestMapping(value = "/login1",method = RequestMethod.POST)
    public String login1(User user){
        //判断用户名密码是否正确
        if("admin".equals(user.getUserName()) && "123".equals(user.getPassword())){
            //登录成功，跳转到index.html页面
            return "index";
        }
        //登录失败，跳转到login.html页面
        return "login";
    }
}
